<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true">
        <el-form-item label="金额总计">
          {{ total.total_amount }}
        </el-form-item>
        <el-form-item label="钻石数量总计">
          {{ total.total_diamond }}
        </el-form-item>
        <el-form-item label="钻石费用总计">
          {{ total.total_fee }}
        </el-form-item>
      </el-form>
    </div>
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户uid">
          <el-input v-model="form.uid" placeholder="用户uid"></el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="form.status" placeholder="订单状态">
            <el-option label="审核中" :value="0" />
            <el-option label="审核通过" :value="1" />
            <el-option label="审核失败" :value="2" />
            <el-option label="已打款" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="form.timeData" type="datetimerange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="defaultTime" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
          <!-- <el-button type="primary" plain @click="onExport2">导出2</el-button> -->
          <el-button type="primary" plain @click="onBatchReview">批量审核</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading" @selection-change="handleSelectionChange"
          ref="multipleTableRef">
          <el-table-column type="selection" width="55" :selectable="selectable" />
          <el-table-column prop="id" label="id序号" align="center" />
          <el-table-column prop="uid" label="用户uid" align="center" width="120" />
          <el-table-column prop="way" label="提现方式" align="center" width="100" />
          <el-table-column prop="amount" label="提现金额" align="center" />
          <el-table-column prop="currency" label="币种" align="center" />
          <el-table-column prop="diamond" label="钻石数量" align="center" />
          <el-table-column prop="fee_price" label="钻石手续费" align="center" />

          <el-table-column prop="bank_no" label="银行卡号" align="center" width="150" />
          <el-table-column prop="bank_name" label="开户行" align="center" width="120" />
          <el-table-column prop="bank_user_name" label="持卡人" align="center" width="120" />
          <el-table-column prop="status" label="审核状态" align="center" width="100" />
          <el-table-column prop="remark" label="备注" align="center" />
          <el-table-column prop="createtime" label="提现时间" :formatter="formatterTime" align="center" width="180" />
          <el-table-column prop="audit_time" label="审核时间" :formatter="formatterTime" align="center" width="180" />
          <el-table-column prop="auditor" label="审核人" align="center" width="160" />
          <el-table-column prop="fail_reason" label="审核原因" align="center" width="160" />
          <el-table-column prop="usdt_address" label="usdt地址" align="center" />
          <el-table-column prop="usdt_network" label="usdt网络" align="center" />
          <el-table-column label="操作" align="center" width="120" fixed="right">
            <template #default="scope">
              <el-button v-if="scope.row.status === '待审核'" type="primary" plain
                @click="onReview(scope.$index, scope.row)">审核</el-button>
              <!-- <el-button
                type="primary"
                @click="onAddCreditCard(scope.$index, scope.row)"
                plain
              >虚拟信用卡</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination :current-page="pageable.pageNum" :page-size="pageable.pageSize" :page-sizes="[10, 25, 50, 100]"
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="pageable.total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </div>
    </div>
    <ReviewForm @cancel="reviewCancel" @closeDialog="reviewCloseDialog" @submitForm="reviewSubmit"
      :dialogFormVisible="reviewFormVisible" :dialogForm="reviewForm" :btnLoading="reviewBtnLoading" />
    <CreditCardForm @cancel="creditCardCancel" @closeDialog="creditCardCloseDialog" @submitForm="creditCardSubmit"
      :dialogFormVisible="creditCardFormVisible" :dialogForm="creditCardForm" :btnLoading="creditCardBtnLoading" />
    <BatchReviewForm @cancel="batchReviewCancel" @closeDialog="batchReviewCloseDialog" @submitForm="batchReviewSubmit"
      :dialogFormVisible="batchReviewFormVisible" :dialogForm="batchReviewForm" :btnLoading="batchReviewBtnLoading" />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, exportToExcel, timestamp,exportOrDownload } from "@/utils/utils";
import {
  withdrawListApi,
  withdrawCheckApi,
  withdrawBankCardApi,
  withdrawBatchCheckApi,
} from "@/api/slotsgame/withdraw/withdraw";
import ReviewForm from "./components/reviewForm.vue";
import CreditCardForm from "./components/creditCardForm.vue";
import BatchReviewForm from "./components/batchReviewForm.vue";
const tableData = ref([]);
const loading = ref(false);
const multipleTableRef = ref("");
// 默认时间
const defaultTime = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59),
]; // '12:00:00', '08:00:00'
const formData = {
  // order_no: "",
  // goods_id: "",
  // status: "",
  timeData: [],
  uid: "",
  status: "",
};
const total = reactive({
  total_amount: 0,
  total_diamond: 0,
  total_fee: 0,
});

const form = reactive({ ...formData });
// 审核
const reviewFormVisible = ref(false);
const reviewBtnLoading = ref(false);

const reviewFormData = {
  id: "",
  status: "",
  remark: "",
};
const reviewForm = reactive({ ...reviewFormData });

// 批量审核
const batchReviewFormVisible = ref(false);
const batchReviewBtnLoading = ref(false);

const batchReviewForm = reactive({ id_list: [], status: "", remark: "" });

// 信用卡
const creditCardFormVisible = ref(false);
const creditCardBtnLoading = ref(false);

const creditCardFormData = {
  id: "",
  credit_card_number: "",
  credit_card_period: "",
  credit_card_code: "",
};
const creditCardForm = reactive({ ...creditCardFormData });

const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    uid: form.uid,
    status: form.status,
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await withdrawListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
    total.total_amount = res.data.extra?.total_amount;
    total.total_diamond = res.data.extra?.total_diamond;
    total.total_fee = res.data.extra?.total_fee;
  } catch (error) {
    loading.value = false;
  }
};
// 批量审核
const onBatchReview = () => {
  console.log(batchReviewForm);
  if (batchReviewForm.id_list.length != 0) {
    batchReviewFormVisible.value = true;
    return;
  }
  ElMessage({
    message: "请选择要审批的数据",
    type: "error",
  });
};

// 批量取消
const batchReviewCancel = () => {
  batchReviewFormVisible.value = false;
  multipleTableRef.value?.clearSelection();
  Object.assign(batchReviewForm, {
    id_list: [],
    status: "",
    remark: "",
  });
};
//批量 关闭
const batchReviewCloseDialog = () => {
  batchReviewFormVisible.value = false;
  multipleTableRef.value?.clearSelection();
  Object.assign(batchReviewForm, {
    id_list: [],
    status: "",
    remark: "",
  });
};
// 批量 提交
const batchReviewSubmit = async (param) => {
  try {
    batchReviewBtnLoading.value = true;
    const res = await withdrawBatchCheckApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    batchReviewBtnLoading.value = false;
    batchReviewFormVisible.value = false;
    getTbaleData();
  } catch (error) {
    batchReviewBtnLoading.value = false;
    batchReviewFormVisible.value = false;
  }
};

// 选中
const handleSelectionChange = (val) => {
  if (val.length != 0) {
    val.forEach((element) => {
      batchReviewForm.id_list.push(element.id);
    });
  } else {
    batchReviewForm.id_list = [];
  }
};
// 是否可选
const selectable = (row, index) => {
  if (row.status === "待审核") {
    return true;
  } else {
    return false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 查询
const onQuery = async () => {
  getTbaleData();
};

// 导出
// rechargeOrderListApi
// withdrawListApi
const onExport = async () => {
  const params = param(); // 获取参数对象
    // params.export = 1;
    // params.pagesize = 9999999;

    // const queryString = Object.keys(params)
    //   .map(key => `${key}=${encodeURIComponent(params[key])}`)
    //   .join("&");

    // const url = `${queryString}`;

    // const res = await withdrawListApi(url, "blob");
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await withdrawListApi(data, "blob");

    exportOrDownload(res, "Slotsgame-提现列表"); 
  // const data = JSON.stringify(Object.assign(param(), {export: 1 }));
  // const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  //   const res = await withdrawListApi(data, "blob");
  //   exportOrDownload(res, "Slotsgame-提现列表.xlsx"); 
  // Object.assign(param(), { export: 1, pagesize: 9999999 });
  // const res = await rechargeOrderListApi(data, "blob");
  // const res = await withdrawListApi(data);
  // exportOrDownload(res, "Slotsgame-提现列表");

  // let head = {
  //   id: "ID",
  //   uid: "用户uid",
  //   way: "提现方式",
  //   amount: "提现金额",
  //   currency: "币种",
  //   diamond: "钻石数量",
  //   fee_price: "钻石手续费",
  //   bank_no: "银行卡号",
  //   bank_name: "开户行",
  //   bank_user_name: "持卡人",
  //   status: "审核状态",
  //   remark: "备注",
  //   usdt_address: "usdt地址",
  //   usdt_network: "usdt网络",
  //   createtime: "提现时间",
  //   audit_time: "审核时间",
  //   auditor: "审核人",
  //   fail_reason: "审核原因",
  // };
  // console.log(res);
  // // exportOrDownload(res,"Slotsgame-提现列表")
  // exportToExcel(res.data.results, head, "Slotsgame-提现列表");
};
// 审核
const onReview = (index, row) => {
  Object.assign(reviewForm, {
    id: row.id,
  });
  reviewFormVisible.value = true;
};
// 取消
const reviewCancel = () => {
  reviewFormVisible.value = false;
  Object.assign(reviewForm, reviewFormData);
};
// 关闭
const reviewCloseDialog = () => {
  reviewFormVisible.value = false;
  Object.assign(reviewForm, reviewFormData);
};
// 提交
const reviewSubmit = async (param) => {
  try {
    reviewBtnLoading.value = true;
    const res = await withdrawCheckApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    reviewFormVisible.value = false;
    reviewBtnLoading.value = false;
    getTbaleData();
  } catch (error) {
    reviewBtnLoading.value = false;
    reviewFormVisible.value = false;
  }
};

// 增加信用卡
const onAddCreditCard = (index, row) => {
  Object.assign(creditCardForm, {
    id: row.id,
  });
  creditCardFormVisible.value = true;
};
// 取消
const creditCardCancel = () => {
  creditCardFormVisible.value = false;
  Object.assign(creditCardForm, creditCardFormData);
};
// 关闭
const creditCardCloseDialog = () => {
  creditCardFormVisible.value = false;
  Object.assign(creditCardForm, creditCardFormData);
};
// 提交
const creditCardSubmit = async (param) => {
  try {
    creditCardBtnLoading.value = true;
    const res = await withdrawBankCardApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    creditCardFormVisible.value = false;
    creditCardBtnLoading.value = false;
    getTbaleData();
  } catch (error) {
    creditCardBtnLoading.value = false;
    creditCardFormVisible.value = false;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
